설명
public class ArrayQueue {
private int front;
private int rear;
private int arrayQueueSize;
private String arrayQueueList[];
public ArrayQueue(int arrayQueueSize){
this.front = -1;
this.rear = -1;
this.arrayQueueList = new String[arrayQueueSize];
this.arrayQueueSize = arrayQueueSize;
}
public boolean isFull() {
return (rear == arrayQueueSize -1 );
}
public boolean isEmpty(){
return (front == rear);
}
public void enQueue(String s){
if(isFull())
System.out.println("길이 " + arrayQueueSize +" 로 생성된 큐가 가득찼습니다");
else{
arrayQueueList[++rear] = s;
System.out.println("enQueue : " + s + " 가 저장 되었습니다.");
}
}
public void deQueue() {
if(isEmpty())
System.out.println("길이 " + arrayQueueSize +" 로 생성된 큐가 비었습니다.");
else{
arrayQueueList[++front] = "";
System.out.println("deQueue 돠었습니다.");
}
}
public void print() {
while( isEmpty()) {
System.out.println("arrayQueueList["+ front +"] = " + arrayQueueList[front]);
++front;
}
}
}
public class ArrayQueueMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayQueue aq = new ArrayQueue(10);
for(int i=1; i<=15; i++)
aq.enQueue(""+i);
for(int i=1; i<=15; i++)
aq.deQueue();
}
}
public class CircleArrayQueue {
private int front;
private int rear;
private int queueSize;
private String circleArrayQueueList[];
public CircleArrayQueue(int queueSize) {
// TODO Auto-generated constructor stub
this.front = 0;
this.rear = 0;
this.queueSize = queueSize;
circleArrayQueueList = new String[queueSize];
}
public boolean isEmpty() {
return (front == rear);
}
public boolean isFull() {
return ((rear + 1) % this.queueSize) == front;
}
public void enQueue(String s) {
if(isFull())
System.out.println("사이즈가 " + this.queueSize + "인 큐가 가득 찼습니다.");
else{
rear = (rear + 1) % this.queueSize;
circleArrayQueueList[rear] = s;
System.out.println("circleArrayQueueList 에 " + s + "가 저장되었습니다.");
}
}
public String deQueue() {
String rtnVal = "";
if(isEmpty())
rtnVal = "큐가 비었습니다.";
else{
front =((front + 1) % this.queueSize);
rtnVal = circleArrayQueueList[front];
}
return rtnVal;
}
}
class CircleArrayQueueMain {
public static void main(String[] args) {
CircleArrayQueue caq = new CircleArrayQueue(10);
for(int i=1; i<=15; i++)
caq.enQueue(""+i);
for(int i=1; i<=15; i++)
System.out.println(caq.deQueue());
}
}
public class QueueNode {
public String data = null;
public QueueNode link = null;
public QueueNode(){
this.data = null;
this.link = null;
}
public QueueNode(String data) {
this.data = data;
}
}
public class LinkedListQueue {
private QueueNode head = null;
private QueueNode tail = null;
int length = 0;
public boolean isEmpty(){
boolean rtnVal = false;
if(this.length < 1) rtnVal = true;
return rtnVal;
}
public void push(String data) {
QueueNode newQueueNode = new QueueNode(data);
System.out.println(data + "가 큐에 저장되었습니다.");
if(isEmpty()) {
this.head = newQueueNode;
}else{
this.tail.link = newQueueNode;
}
this.tail = newQueueNode;
this.length++;
}
public String pop() {
String rtnVal = "";
if (isEmpty()) {
rtnVal = "큐가 비었습니다.";
}else{
QueueNode tmpNode = this.head;
rtnVal = tmpNode.data + " 가 큐에서 출력되었습니다.";
if (this.length == 1) {
this.head = null;
this.tail = null;
}else{
this.head = tmpNode.link;
tmpNode.link = null;
}
this.length--;
}
return rtnVal;
}
}
public class LinkedListQueueMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedListQueue llq = new LinkedListQueue();
// lls.pop();
for(int i=1 ; i < 10; i ++){
llq.push(" " + i);
}
while (llq.length > 0) {
System.out.println(llq.pop());
}
for(int i=1 ; i < 10; i ++){
System.out.println(llq.pop());
}
}
}
큐와 비슷하지만 노드에 2개의 링크가 존재 하며 큐의 양쪽끝 모두 에서 입력삭제가 가능한 자료구조 (DECK) 구현
public class DoubleLinkedListDeck {
private DQueueNode front;
private DQueueNode rear;
private int length;
public DoubleLinkedListDeck() {
this.front = null;
this.rear = null;
this.length = 0;
}
public boolean isEmpty() {
return this.front == null;
}
public int getLength() {
return length;
}
public void insertFront(String data) {
DQueueNode newNode = new DQueueNode(data);
if (isEmpty()) {
this.front = newNode;
this.rear = newNode;
newNode.nextNode = null;
newNode.prevNode = null;
}else{
this.front.prevNode = newNode;
newNode.nextNode = this.front;
newNode.prevNode = null;
this.front = newNode;
}
System.out.println("DECK 에 insertFront()로 \" " + data + " \" 가 입력되었습니다.");
}
public String deleteFront(){
String rtnVal="";
if (isEmpty())
rtnVal = "DoubleLinkedListDeck 이 비었습니다";
else{
rtnVal = this.front.data + " 가 DoubleLinkedListDeck 에서 deleteFront() 로 삭제되었습니다.";
if(this.front.nextNode == null){
this.front = null;
this.rear = null;
}else{
this.front.nextNode.prevNode = null;
this.front = front.nextNode;
// this.front = front.nextNode;
// this.front.prev = null;
}
}
return rtnVal;
}
public void insertRear(String data) {
DQueueNode newNode = new DQueueNode(data);
if (isEmpty()) {
this.front = newNode;
this.rear = newNode;
newNode.nextNode = null;
newNode.prevNode = null;
}else{
this.rear.nextNode = newNode;
newNode.nextNode = null;
newNode.prevNode = this.rear;
this.rear = newNode;
}
}
public String deleteRear(){
String rtnVal = "";
if (isEmpty())
rtnVal = "DoubleLinkedListDeck 이 비었습니다";
else{
rtnVal = this.rear.data + " 가 DoubleLinkedListDeck 에서 deleteRear() 로 삭제되었습니다.";
if (this.rear.prevNode == null) {
this.front = null;
this.rear = null;
}else{
this.rear.prevNode.nextNode = null;
this.rear = rear.prevNode;
}
}
return rtnVal;
}
}
public class DoubleLinkedListDeckMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DoubleLinkedListDeck dlld = new DoubleLinkedListDeck();
dlld.deleteFront();
dlld.deleteRear();
for(int i=1; i < 10; i++){
dlld.insertFront(" " + i);
}
for(int i=1; i < 15; i++){
//System.out.println(dlld.deleteRear());
System.out.println(dlld.deleteFront());
}
}
}